package com.wanban.adutils.advertisers.admob

import android.app.Activity
import android.util.Log
import com.google.android.gms.ads.*
import com.google.android.gms.ads.appopen.AppOpenAd
import com.wanban.adutils.advertisers.BaseAdLoader
import com.wanban.adutils.AdUtils
import com.wanban.adutils.db.bean.AdInfo
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.tools.AdConstants
import java.util.*

class AdmobSplashAdUtils : BaseAdLoader() {

    private var loadTime: Long = 0
    private var appOpenAd: AppOpenAd? = null

    override fun loadAdNew(activity: Activity, adInfo: AdInfo, callback: OnLoadAdCallback) {
        super.loadAdNew(activity, adInfo, callback)
        val loadCallback = object : AppOpenAd.AppOpenAdLoadCallback() {
            override fun onAdLoaded(appOpenAd: AppOpenAd) {
                super.onAdLoaded(appOpenAd)
                setAdLoadSuccess()
                Log.e(AdConstants.TAG, "AdmobSplashAdUtils -> onAdLoaded...")
                if (!isTimeout) {
                    sdkLoadFinish()
                }
                // 记录获取到广告的时间
                loadTime = System.currentTimeMillis()

                this@AdmobSplashAdUtils.appOpenAd = appOpenAd

                if (!isTimeout) {
                    callback.onAdLoaded(appOpenAd)
                }
            }

            override fun onAdFailedToLoad(loadAdError: LoadAdError) {
                super.onAdFailedToLoad(loadAdError)
                Log.e(AdConstants.TAG, "AdmobSplashAdUtils -> onAdFailedToLoad...")
                if (isTimeout) {
                    destroy()
                    return
                }
                sdkLoadFinish()
                val code = getAdmobErrorCode(loadAdError)
                analysisErrorCode(adInfo, code)
                callback.onAdLoadFailed(code)
                destroy()
            }
        }
        AppOpenAd.load(AdUtils.appContext.applicationContext, realUnitId, AdUtils.getAdRequest(), loadCallback)
    }


    override fun showAd(activity: Activity) {
        super.showAd(activity)
        activity.runOnUiThread {
            if (isAdAvailable()) {
                appOpenAd?.fullScreenContentCallback = object : FullScreenContentCallback() {

                    override fun onAdDismissedFullScreenContent() {
                        super.onAdDismissedFullScreenContent()
                        setAdLoadShowing(false)
                        mOnLoadAdCallback?.onAdComplete()
                        destroy()
                    }

                    override fun onAdFailedToShowFullScreenContent(p0: AdError) {
                        super.onAdFailedToShowFullScreenContent(p0)
                        sdkLoadFinish()
                        setAdLoadShowing(false)
                        val code = p0.code
                        mOnLoadAdCallback?.onAdLoadFailed(code)
                        destroy()
                    }

                    override fun onAdShowedFullScreenContent() {
                        super.onAdShowedFullScreenContent()
                        setAdLoadShowing(true)
                        mOnLoadAdCallback?.onAdOpened()
                    }
                }
                appOpenAd?.show(activity)
            } else {
                mOnLoadAdCallback?.onAdLoadFailed(AdConstants.ERROR_CODE_AD_CONTENT_NULL)
                destroy()
            }
        }
    }

    override fun isAdLoadSuccess(): Boolean {
        return isAdAvailable()
    }


    private fun isAdAvailable(): Boolean {
        return appOpenAd != null && wasLoadTimeLessThanNHoursAgo()
    }

    // 4小时失效
    private fun wasLoadTimeLessThanNHoursAgo(): Boolean {
        val dateDifference = Date().time - loadTime
        val numMilliSecondsPerHour: Long = 3600000
        return dateDifference < numMilliSecondsPerHour * 1
    }

    override fun destroy() {
        super.destroy()
        appOpenAd = null
    }
}